package com.wingstudio.springcloud.controller;

import com.wingstudio.springcloud.entities.Payment;
import com.wingstudio.springcloud.result.R;
import com.wingstudio.springcloud.service.Impl.PaymentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * @Author ITcz
 * @Data 2020-12-07 - 22:05
 */

@RestController
@RequestMapping("/payment")
public class PaymentController {

    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(PaymentController.class);

    @Autowired
    private PaymentServiceImpl paymentService;

    @Value("${server.port}")
    private String serverPort;

    @Autowired
    private DiscoveryClient discoveryClient;

    @PostMapping("/create" )
    public R create(@RequestBody Payment payment){
        int result = paymentService.create(payment);
        log.info("******插入结果" + payment);
        if(result > 0) {
            return R.ok("插入成功,serverPort: " + serverPort);
        }else {
            return R.error("插入失败");
        }
    }

    @GetMapping("/get/{id}")
    public R getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("******获取结果" + payment);
        if(payment !=  null) {
            HashMap res = new HashMap();
            res.put("payment", payment);
            res.put("serverPort", serverPort);
            return R.ok(res);
        }else {
            return R.error(444, "没有对应记录，查询ID："+id);
        }
    }

    @GetMapping("/getForEntity/{id}")
    public R getPaymentById2(@PathVariable("id") Integer id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("******获取结果" + payment);
        if(payment !=  null) {
            HashMap res = new HashMap();
            res.put("payment", payment);
            res.put("serverPort: ", serverPort);
            return R.ok(res);
        }else {
            return R.error(445, "没有对应记录，查询ID："+id);
        }
    }
    
    @GetMapping(value = "/discovery")
    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        for (String element:
             services) {
            log.info("*******element: " + element);
        }

        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance :
               instances ) {
            log.info(instance.getServiceId() + "\t"+instance.getHost()+ "\t" + instance.getPort()+"\t" + instance.getUri());
        }
        return this.discoveryClient;
    }

    @GetMapping("/lb")
    public String getPaymentLB(){
        return serverPort;
    }

    @GetMapping("/feign/timeout")
    public String feignTimeout(){
        try{
            TimeUnit.SECONDS.sleep(3);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return serverPort;
    }
    
}
